//
//  MXDetialBeltTableView.m
//  MXUserSys
//
//  Created by 王启颖 on 2018/10/15.
//  Copyright © 2018年 王启颖. All rights reserved.
//

#import "MXDetialBeltTableView.h"
#import "MXDetialBeltCell.h"
#import "WLDecimalKeyboard.h"
@interface MXDetialBeltTableView () <UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic ,strong) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *paramsArr;
@property (strong, nonatomic) UITextField *textField;
@end

@implementation MXDetialBeltTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - KSafeAreaTopHeight - 44)];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = MX_GRAY_COLOR;
        self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        self.tableView.transform = CGAffineTransformMakeScale (1,-1);
        [self addSubview:self.tableView];
        
        WLDecimalKeyboard *inputView = [[WLDecimalKeyboard alloc] init];
        inputView.done = ^{
            [self addBeltDetialRequest:self.textField.text];
        };
        inputView.isResign = YES;
        
        self.textField = [[UITextField alloc]initWithFrame:CGRectMake(30, kScreenHeight-44-KSafeAreaTopHeight, 300, 44)];
        self.textField.delegate = self;
        self.textField.backgroundColor = [UIColor whiteColor];
        self.textField.placeholder = @"请输入xxxxx";
        self.textField.inputView = inputView;
        [self.textField reloadInputViews];
        [self addSubview:self.textField];
        
        
        
    }
    return self;
}

-(void)setPid:(NSString *)pid{
    _pid = pid;
    [self getBeltDetialRequest];
}


#pragma mark - UITextFieldDelegate -
/// 设置自定义键盘后，delegate 不会被调用？
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSLog(@"%@", [textField.text stringByReplacingCharactersInRange:range withString:string]);
    
    return YES;
}


#pragma mark - UITableViewDelegate -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //    return [self.paramsArr count];
    return [self.paramsArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MXDetialBeltCell *cell = [MXDetialBeltCell cellWithTableView:tableView];
    
    NSDictionary *dic = self.paramsArr[self.paramsArr.count - indexPath.row - 1];
    cell.numLab.text = [NSString stringWithFormat:@"第%ld机",self.paramsArr.count - indexPath.row];
    cell.outputNumLab.text = [NSString stringWithFormat:@"%@",dic[@"outputNum"]];
    
    cell.contentView.transform = CGAffineTransformMakeScale (1,-1);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44.f;
}

#pragma mark - 懒加载 -
-(NSMutableArray *)paramsArr{
    if (!_paramsArr) {
        _paramsArr = [[NSMutableArray alloc]init];
    }
    return _paramsArr;
}

#pragma mark - 网络请求 -
-(void)getBeltDetialRequest{
    [SVProgressHUD show];
    
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:self.pid,@"pid", nil];
    
    [[MXHttpManagerClass HttpRequstManager] PostURL:getBeltdetailByPid params:params isHearderparam:YES success:^(NSInteger code, id result) {
        [SVProgressHUD dismiss];
        
        if (code == 0) {
            NSLog(@"%@",result);
            
            self.paramsArr = result[@"message"];
            
            
            [self.tableView reloadData];
        }else{
            [SVProgressHUD showImage:NULL_IMAGE status:@"账号密码错误"];
        }
        
    } fail:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

-(void)addBeltDetialRequest:(NSString *)output_num{
    [SVProgressHUD show];
    
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:self.pid,@"pid",output_num,@"output_num", nil];
    
    [[MXHttpManagerClass HttpRequstManager] PostURL:addBeltDetial params:params isHearderparam:YES success:^(NSInteger code, id result) {
        [SVProgressHUD dismiss];
        
        if (code == 0) { 
            self.paramsArr = result[@"message"];
            [self.textField setText:@""];
         
            [self getBeltDetialRequest];
        }else{
            [SVProgressHUD showImage:NULL_IMAGE status:@"账号密码错误"];
        }
        
    } fail:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}


@end

